[<<Previous Entry]
[^^Up^^]
[Next Entry>>]
[Menu]
[About The Guide]
Expressions
The autoincrement operator has a little extra built-in magic
to it. If you increment a variable that is numeric, or that
has ever been used in a numeric context, you get a normal
increment. If, however, the variable has only been used in
string contexts since it was set, and has a value that is
not null and matches the pattern /^[a-zA-Z]*[0-9]*$/, the
increment is done as a string, preserving each character
within its range, with carry:
print ++($foo = '99'); # prints '100'
print ++($foo = 'a0'); # prints 'a1'
print ++($foo = 'Az'); # prints 'Ba'
print ++($foo = 'zz'); # prints 'aaa'
The autodecrement is not magical.
The range operator (in an array context) makes use of the
magical autoincrement algorithm if the minimum and maximum
are strings. You can say
@alphabet = ('A' .. 'Z');
to get all the letters of the alphabet, or
$hexdigit = (0 .. 9, 'a' .. 'f')[$num & 15];
to get a hexadecimal digit, or
@z2 = ('01' .. '31'); print @z2[$mday];
to get dates with leading zeros. (If the final value speci-
fied is not in the sequence that the magical increment would
produce, the sequence goes until the next value would be
longer than the final value specified.)
The || and && operators differ from C's in that, rather than
returning 0 or 1, they return the last value evaluated.
Thus, a portable way to find out the home directory might
be:
$home = $ENV{'HOME'} || $ENV{'LOGDIR'} ||
(getpwuid($<))[7] || die "You're homeless!\n";
Along with the literals and variables mentioned earlier, the
operations in the following section can serve as terms in an
expression. Some of these operations take a LIST as an
argument. Such a list can consist of any combination of
scalar arguments or array values; the array values will be
included in the list as if each individual element were
interpolated at that point in the list, forming a longer
single-dimensional array value. Elements of the LIST should
be separated by commas. If an operation is listed both with
and without parentheses around its arguments, it means you
can either use it as a unary operator or as a function call.
To use it as a function call, the next token on the same
line must be a left parenthesis. (There may be intervening
white space.) Such a function then has highest precedence,
as you would expect from a function. If any token other
than a left parenthesis follows, then it is a unary opera-
tor, with a precedence depending only on whether it is a
LIST operator or not. LIST operators have lowest pre-
cedence. All other unary operators have a precedence
greater than relational operators but less than arithmetic
operators. See the section on Precedence.
This page created by ng2html v1.05, the Norton guide to HTML conversion utility.
Written by Dave Pearson